package adb;

import static adb.ADBView.WriteFileTMP;
import static adb.ADBView.aaptVariable;
import static adb.ADBView.adbVariable;
import static adb.ADBView.jProgressBarDoInBackground;
import static adb.ADBView.jTextAreaStatus;
import static adb.ADBView.listApps;
import static adb.ADBView.output;
import static adb.ADBView.readSavedADB;
import static adb.ADBView.removeFileinPath;
import static adb.ADBView.showPCTree;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.swing.JFileChooser;
import javax.swing.ListSelectionModel;
import javax.swing.SwingWorker;

public class LoadingUbuntu extends SwingWorker<Void, String> {

    @Override
    protected Void doInBackground() throws Exception {

        int progress = 0;
        jProgressBarDoInBackground.setValue(progress);
        getADBpath();
        //System.out.print(adb+" <<< adb after getADBpath\n");
        listApps.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        showPCTree();
        
        progress = 30;
        jProgressBarDoInBackground.setValue(progress);
        String command = adbVariable + " devices";
        if (command.charAt(0) != '0') {
            String[] cc = command.split(" ");
            try {
                String line = "";
                ProcessBuilder pb = new ProcessBuilder(cc);
                pb.redirectErrorStream(true);
                Process process = pb.start();
                BufferedReader inStreamReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                progress = 50;
                jProgressBarDoInBackground.setValue(progress);
                while ((line = inStreamReader.readLine()) != null && !"".equals(line)) {
                    if (line.length() > 0) {
                        //out = out + line + "\n";
                        output = output + line + "\n";
                        jTextAreaStatus.setText(output);
                        //System.out.print(line+"\n");
                        progress = progress + 10;
                        jProgressBarDoInBackground.setValue(progress);
                    }
                }
            } catch (IOException ex) {
                output = output + ex + "----ex2\n";
                jTextAreaStatus.setText(output);
            }
        }
        //jTextAreaStatus.setText(output);
        jProgressBarDoInBackground.setValue(0);
        return null;
    }

    
    public static void getADBpath() {
        // get current jAR PATH
        String ADBJarPath = getJarpath();
        if (ADBJarPath.contains(".jar")) {
            ADBJarPath = removeFileinPath(ADBJarPath);
//output=output+ADBJarPath+"<<ADBJarPath4444\n";
//jTextAreaStatus.setText(output);
        }
        // CHECK IF ADB PLACE IN CURRENT JAR PATH
        File adbfullpath = null;
        adbfullpath = new File("/"+ADBJarPath + "adb");
        //jTextAreaStatus.setText(output=output+adbfullpath+"adbfullpath\n");
        if (adbfullpath.isFile()) {
//output=output+adbfullpath.isFile()+"<<<adbfullpath.isFile()\n";
//jTextAreaStatus.setText(output);
//output=output+adbfullpath.getPath()+"<<<adbfullpath1122\n";
//jTextAreaStatus.setText(output);
            if(ADBJarPath.charAt(0)=='/')
            {ADBJarPath=ADBJarPath.substring(1);
//            output=output+ADBJarPath+"<<<ADBJarPath1133\n";
//jTextAreaStatus.setText(output);
            }
//output=output+ADBJarPath+"<<<adbVariable000\n";
//jTextAreaStatus.setText(output);
                int k=0;
                while(ADBJarPath.charAt(k)!='/'){
                    
                    k++;
//output=output+ADBJarPath+"<<<ADBJarPath.charAt(k)!='/'\n";
//jTextAreaStatus.setText(output);
                }
                ADBJarPath = ADBJarPath.substring(k+1);
//output=output+ADBJarPath+"<<<ADBJarPath9999\n";
//jTextAreaStatus.setText(output);
                int j=0;
                while(ADBJarPath.charAt(j)!='/'){
                    j++; 
                    
//output=output+ADBJarPath+"<<<ADBJarPath.charAt(k)!='/'\n";
//jTextAreaStatus.setText(output);
                }
                ADBJarPath = ADBJarPath.substring(j+1);
//output=output+ADBJarPath+"<<<ADBJarPath9000\n";
//jTextAreaStatus.setText(output);
            adbVariable = ""+ADBJarPath+"./adb";
            aaptVariable =""+ ADBJarPath+"./aapt";
//output=output+adbVariable+"<<<adbVariable111\n";
//jTextAreaStatus.setText(output);

        //jTextAreaStatus.setText(output=output+adb+"adb getADBfolderAndsetFullADBpath\n");
        } else {// IF ADB NOT IN CURRENT PATH, CHECK IN DATA .ADB
            String ADBpathFromData = readSavedADB();
        
            if (ADBpathFromData.length() > 1) {
        
                File adbfullpath2 = null;
                adbfullpath2 = new File(ADBpathFromData + "adb");
//output=output+adbfullpath2+"<<<adbfullpath2\n";
//jTextAreaStatus.setText(output);
                if (adbfullpath2.isFile()) {
//output=output+adbfullpath2.isFile()+"<<<adbfullpath2.isFile()\n";
//jTextAreaStatus.setText(output);
                    getADBfolderAndsetFullADBpathUbuntu(ADBpathFromData);
//output=output+adbVariable+"<<<adbVariable22222222\n";
//jTextAreaStatus.setText(output);
                }else openFileChooserAndWrite();
        
            } else {
                openFileChooserAndWrite();
            }
        }
    }
    
    
    public static void openFileChooserAndWrite() {
        jTextAreaStatus.setText("Select adb...\n");
        JFileChooser openAdb = new JFileChooser();
        openAdb.setMultiSelectionEnabled(false);
        openAdb.setName("Select ADB file");
        openAdb.setDialogTitle("Select adb file");
        int state = openAdb.showOpenDialog(ADBView.jLabelToLeft);
        if (state == 1) {
            jTextAreaStatus.setText(output = output + "Select adb first.\nExit and try again.\n");
        }
        adbVariable = openAdb.getSelectedFile().toString();
        int i = adbVariable.length() - 1;
            while (adbVariable.charAt(i) != '/') {
                adbVariable = adbVariable.substring(0, i);
                i--;
            }
        WriteFileTMP(adbVariable, 1);
        getADBfolderAndsetFullADBpathUbuntu(adbVariable);
//output=output+adbVariable+"<<<adbVariable5555\n";
//jTextAreaStatus.setText(output);
        //jTextAreaStatus.setText(output = output + adb+"<<<adb. end of filechooser\n");
    }
    public static String getJarpath(){
    String ADBJarPath = ADBView.class.getProtectionDomain().getCodeSource().getLocation().getPath();
        try {
            ADBJarPath = URLDecoder.decode(ADBJarPath, "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            output = output + ex + "----ex17\n";
            jTextAreaStatus.setText(output);
        }
        if (ADBJarPath.charAt(0) == '/') {
            ADBJarPath = ADBJarPath.substring(1);
//            output = output + ADBJarPath + "<<<ADBJarPath\n";
//            jTextAreaStatus.setText(output);
        }
        return ADBJarPath; 
    }
    public static void getADBfolderAndsetFullADBpathUbuntu(String inputPath){

            //inputPath=inputPath.substring(2);
//output=output+adbVariable+"<<<adbVariable6666\n";
//jTextAreaStatus.setText(output);
                int k=0;
                while(adbVariable.charAt(k)!='/'){
                    adbVariable = adbVariable.substring(k+1);
                    k++;
                }
//output=output+adbVariable+"<<<adbVariable7777\n";
//jTextAreaStatus.setText(output);
                int j=0;
                while(adbVariable.charAt(j)!='/'){
                    adbVariable = adbVariable.substring(j+1);
                    j++;
                }
            adbVariable = "\""+inputPath+"./adb\"";
            aaptVariable ="\""+ inputPath+"./aapt\"";
//output=output+adbVariable+"<<<adbVariable8888\n";
//jTextAreaStatus.setText(output);
        
    }
    
    @Override
    protected void done() {
        jProgressBarDoInBackground.setValue(0);
    }
}
